import whisper

model = whisper.load_model("/home/peter/Public/whisper_data/base.pt")

# load audio and pad/trim it to fit 30 seconds
# audio = whisper.load_audio("audio.mp3")
# audio = whisper.load_audio("/home/peter/Public/audio_data/1730603911933.mp3")
audio = whisper.load_audio("/home/peter/Public/audio_data/1730604585033.mp3")
# whisper.pad_or_trim(audio) 方法会对音频进行填充或裁剪，使其长度符合 Whisper 模型的预期输入大小。具体来说，Whisper 模型设计时的输入长度是基于最大约 30 秒的音频段。这是因为模型的架构和训练数据的设计会假设音频输入的最大长度。这样设计的好处是可以在合理的内存和计算资源范围内进行高效的处理。
audio = whisper.pad_or_trim(audio)
# make log-Mel spectrogram and move to the same device as the model
mel = whisper.log_mel_spectrogram(audio).to(model.device)

# detect the spoken language
_, probs = model.detect_language(mel)
print(f"Detected language: {max(probs, key=probs.get)}")

# decode the audio
options = whisper.DecodingOptions()
result = whisper.decode(model, mel, options)

# print the recognized text
print(result.text)